

function C(a,b){
    var c=document.createElement(b);
    a.appendChild(c);
    return c;
}

String.prototype.rpl = function(src,des){
	var str = this;
	if(typeof src=="object"){
		for(var i=0;i<src.length;i++){
			var reg=new RegExp("\\$"+src[i]+"\\$","g");
			str = str.replace(reg,des[i]);
		}
	}else{
		var reg=new RegExp("\\$"+src+"\\$","g");
		str = str.replace(reg,des);
	}
	return str;
};

String.prototype.sub_str = function(len){
	var str = this;
	if(str.replace(/[^\x00-\xff]/gi,'xx').length<=len) return str;
	var temp_len=len;  
	str=str.substr(0,len);
	while(str.replace(/[^\x00-\xff]/gi,'xx').length>len){
		str=str.substr(0,--temp_len);
	}
	return str;
}

function ShowMask() {
	if($("#dialogmask").length==0){
		obj = C(document.body,'div');
		obj.id = 'dialogmask';
	}
	var mask = $("#dialogmask");
	//var mHeight = (document.documentElement.scrollHeight);
	var mHeight = (document.body.scrollHeight);
	//var mHeight = document.body.clientHeight;
    if (mHeight<document.documentElement.clientHeight){mHeight = document.documentElement.clientHeight;}
    //alert(mHeight);
	mask.css('height',mHeight);
	mask.attr('class','dialog_mask');
	mask.bind("keydown",function(){return false;});
	mask.bind("keypress",function(){return false;});
	mask.css('display','block');
}

function HideMask(){
	var mask = $("#dialogmask");
	mask.css('display','none');
}

var Alert = (function () {
    var remindintervalId = null;
    var rt = 0; rh = 0;
    var DialogMouseDown = false;
    var CursorOldX = 0, CursorOldY = 0;
    var OnDialogColse = null;
    function closeRemind() {
        $('#remind').css('display', 'none');
        RemindFlag = true;
    }

    function fOnDialogMouseDown(e) {
        if (e.target.id == "dialog_close" || e.target.id == "no_move" || e.target.type == "text" || e.target.type == "button" || e.target.type == "textarea" || e.target.id == "timeline_id" || e.target.id == "font" || e.target.id == "content" || e.target.type == "select") {
            return;
        }
        CursorOldX = e.screenX;
        CursorOldY = e.screenY;
        DialogMouseDown = true;
        return false;
    }

    function fOnDialogMove(e) {
        if (DialogMouseDown) {
            var o = $("#dialog");
            if (o.length != 0) {
                var l = parseInt(o.css('left')) + e.screenX - CursorOldX;
                var t = parseInt(o.css('top')) + e.screenY - CursorOldY;

                o.css('left', l);
                o.css('top', t);
                CursorOldX = e.screenX;
                CursorOldY = e.screenY;

            }
        }
    }

    function fOnDialogMouseUp(e) {
        DialogMouseDown = false;
    }

    function ScollPostion() {//滚动条位置
        var t, l, w, h;
        if (document.documentElement && document.documentElement.scrollTop) {
            t = document.documentElement.scrollTop;
            l = document.documentElement.scrollLeft;
            w = document.documentElement.scrollWidth;
            h = document.documentElement.scrollHeight;
        } else if (document.body) {
            t = document.body.scrollTop;
            l = document.body.scrollLeft;
            w = document.body.scrollWidth;
            h = document.body.scrollHeight;
        }
        return { top: t, left: l, width: w, height: h };
    }

    var iobj = null;
    function Hidediv() {
        clearHide();
        iobj = window.setTimeout(function () {
            $("#dialog").fadeOut(1000, function () { });
        }, 100);
    }
    function clearHide() {
        if (iobj != null) clearInterval(iobj);
    }



    return {
        show: //弹出消息
	function (msg) {
	    alert(msg);
	},

        dialog: //弹出对话框
	function (title, content, width, height, l, t, closefun, ismask, movehide) {

	    if (ismask == 1) {
	        ShowMask();
	    }
	    OnDialogColse = null;
	    if (closefun != null) OnDialogColse = closefun;
	    var dialog = $("#dialog");
	    if (dialog.length == 0) {
	        var baseobj = C(document.body, "DIV");
	        baseobj.id = "dialog";
	        dialog = $("#dialog");
	    } else {
	        $("#dialog").remove();
	        var baseobj = C(document.body, "DIV");
	        baseobj.id = "dialog";
	        dialog = $("#dialog");
	    }
	    dialog.attr('class', 'dialog');
	    if (width != null) {
	        dialog.css('width', width + 'px');
	    }
	    if (height != null)
	        dialog.css('height', height + 'px');
	    //var tb = document.body;
	    var tb = document.documentElement;
	    var tbheight = document.documentElement.scrollTop;
	    var ccc = ScollPostion().top;
	    //if (l == null) l = (tb.clientWidth - width) / 2 + tb.scrollLeft;
		if (l == null) l = (document.body.clientWidth - width) / 2 + tb.scrollLeft;
	    //alert(ccc);
	    //if(t==null) t = ( tb.clientHeight - height ) / 2 + ccc;
		var scrollTop=0;
	    if(document.documentElement&&document.documentElement.scrollTop)
	    {
	        scrollTop=document.documentElement.scrollTop;
	    }
	    else if(document.body)
	    {
	        scrollTop=document.body.scrollTop;
	    }
	    
	    var clientHeight=0;
	    if(document.body.clientHeight&&document.documentElement.clientHeight)
	    {
	        var clientHeight = (document.body.clientHeight<document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;        
	    }
	    else
	    {
	        var clientHeight = (document.body.clientHeight>document.documentElement.clientHeight)?document.body.clientHeight:document.documentElement.clientHeight;    
	    }
		//alert(document.body.scrollHeight);
	    //if (t == null) t = (document.body.clientHeight - height) / 2;
	    if (t == null) t = (clientHeight - height) / 2 + scrollTop;
	    //alert(ccc);
	    dialog.css('left', l);
	    dialog.css('top', t);
	    html = '<iframe style="background:none;border:0px;position:absolute;filter:alpha(opacity=0);-moz-opacity:0; left:1px;top:2px;z-index:-1;width:' + width + 'px;height:' + height + 'px;"></iframe><div id="dialog_til_bar"  >$content$</div>';
	    html = html.rpl("title", title);
	    html = html.rpl("content", content);
	    dialog.html(html);
	    var dialog_til = $("#dialog_til_bar");
	    //dialog_til.bind("mousedown", fOnDialogMouseDown);
	    //dialog_til.bind("mouseup", fOnDialogMouseUp);
	    if (movehide == 1) {
	        dialog_til.bind("mouseout", Hidediv);
	        dialog_til.bind("mouseover", clearHide);
	    }
	    //$(document).bind("mousemove", fOnDialogMove);
	    dialog.show();

	},

        updateDialogCtx:
	function (html) {
	    $("#dialog_content").html(html);
	},

        closedialog:
	function (t_href) {
	    if (OnDialogColse != null) OnDialogColse();
	    OnDialogColse = null;
	    HideMask();
	    var dialog = $("#dialog");
	    if (dialog.length != 0)
	        dialog.css('display', 'none');
	    if (typeof (t_href) != "undefined" ) {
	        window.location.href = t_href;
	    } else {
	        return false;
	    }
	}

    };

})();


//去掉字符串两边的空格。如果字符串为null,则返回"" 
function trim(str){ 
    return rtrim(ltrim(str)); 
} 

//去掉字符串左边的空格 
function ltrim(s){ 
    if(s == null) return ""; 
    var whitespace = new String(" \t\n\r"); 
    var str = new String(s); 
    if (whitespace.indexOf(str.charAt(0)) != -1) { 
        var j=0, i = str.length; 
        while (j < i && whitespace.indexOf(str.charAt(j)) != -1){ 
            j++; 
        } 
        str = str.substring(j, i); 
    } 
    return str; 
} 

//去掉字符串右边的空格 
function rtrim(s){ 
    if(s == null) return ""; 
    var whitespace = new String(" \t\n\r"); 
    var str = new String(s); 
    if (whitespace.indexOf(str.charAt(str.length-1)) != -1){ 
        var i = str.length - 1; 
        while (i >= 0 && whitespace.indexOf(str.charAt(i)) != -1){ 
            i--; 
        } 
        str = str.substring(0, i+1); 
    } 
    return str; 
}

function getCookieVal(offset){
	var endstr=document.cookie.indexOf(";",offset);
	if(endstr==-1){endstr=document.cookie.length;}
	return unescape(document.cookie.substring(offset,endstr));
}

function getCookie(name){
	var arg=name+"=";
	var alen=arg.length;
	var clen=document.cookie.length;
	var i=0;
	while(i<clen){
		var j=i+alen;
		if(document.cookie.substring(i,j)==arg){
			return getCookieVal(j);
		}
		i=document.cookie.indexOf(" ",i)+1;
		if(i==0)break;
	}
	return"";
}

function setCookie(name,value,expires,path,domain,secure){
	document.cookie=name+"="+escape(value)+((expires)?"; expires="+expires:"")+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+((secure)?"; secure":"");
}

function deleteCookie(name,path,domain){
	if(getCookie(name)){
		document.cookie=name+"="+((path)?"; path="+path:"")+((domain)?"; domain="+domain:"")+"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

function refreshCaptcha(o){
	o.src = "/captcha.php?" + Math.round(Math.random()*10000);
}

Date.prototype.getWeekNumber = function() {
	var d = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
	var DoW = d.getDay();
	d.setDate(d.getDate() - (DoW + 6) % 7 + 3); // Nearest Thu
	var ms = d.valueOf(); // GMT
	d.setMonth(0);
	d.setDate(4); // Thu in Week 1
	return Math.round((ms - d.valueOf()) / (7 * 864e5)) + 1;
};

Date.prototype.getDayOfYear = function() {
	var now = new Date(this.getFullYear(), this.getMonth(), this.getDate(), 0, 0, 0);
	var then = new Date(this.getFullYear(), 0, 0, 0, 0, 0);
	var time = now - then;
	return Math.floor(time / Date.DAY);
};

Date.prototype.format = function(str) {
	var m = this.getMonth();
	var d = this.getDate();
	var y = this.getFullYear();
	var wn = this.getWeekNumber();
	var w = this.getDay();
	var s = {};
	var hr = this.getHours();
	var pm = (hr >= 12);
	var ir = (pm) ? (hr - 12) : hr;
	var dy = this.getDayOfYear();
	if (ir == 0)
		ir = 12;
	var min = this.getMinutes();
	var sec = this.getSeconds();	
	// FIXME: %c : preferred date and time representation for the current locale
	s["%C"] = 1 + Math.floor(y / 100); // the century number
	s["%d"] = (d < 10) ? ("0" + d) : d; // the day of the month (range 01 to 31)
	s["%e"] = d; // the day of the month (range 1 to 31)
	// FIXME: %D : american date style: %m/%d/%y
	// FIXME: %E, %F, %G, %g, %h (man strftime)
	s["%H"] = (hr < 10) ? ("0" + hr) : hr; // hour, range 00 to 23 (24h format)
	s["%I"] = (ir < 10) ? ("0" + ir) : ir; // hour, range 01 to 12 (12h format)
	s["%j"] = (dy < 100) ? ((dy < 10) ? ("00" + dy) : ("0" + dy)) : dy; // day of the year (range 001 to 366)
	s["%k"] = hr;		// hour, range 0 to 23 (24h format)
	s["%l"] = ir;		// hour, range 1 to 12 (12h format)
	s["%m"] = (m < 9) ? ("0" + (1+m)) : (1+m); // month, range 01 to 12
	s["%M"] = (min < 10) ? ("0" + min) : min; // minute, range 00 to 59
	s["%n"] = "\n";		// a newline character
	s["%p"] = pm ? "PM" : "AM";
	s["%P"] = pm ? "pm" : "am";
	// FIXME: %r : the time in am/pm notation %I:%M:%S %p
	// FIXME: %R : the time in 24-hour notation %H:%M
	s["%s"] = Math.floor(this.getTime() / 1000);
	s["%S"] = (sec < 10) ? ("0" + sec) : sec; // seconds, range 00 to 59
	s["%t"] = "\t";		// a tab character
	// FIXME: %T : the time in 24-hour notation (%H:%M:%S)
	s["%U"] = s["%W"] = s["%V"] = (wn < 10) ? ("0" + wn) : wn;
	s["%u"] = w + 1;	// the day of the week (range 1 to 7, 1 = MON)
	s["%w"] = w;		// the day of the week (range 0 to 6, 0 = SUN)
	// FIXME: %x : preferred date representation for the current locale without the time
	// FIXME: %X : preferred time representation for the current locale without the date
	s["%y"] = ('' + y).substr(2, 2); // year without the century (range 00 to 99)
	s["%Y"] = y;		// year with the century
	s["%%"] = "%";		// a literal '%' character

	var re = /%./g;

	var a = str.match(re);
	for (var i = 0; i < a.length; i++) {
		var tmp = s[a[i]];
		if (tmp) {
			
			re = new RegExp(a[i], 'g');
			str = str.replace(re, tmp);
		}
	}
	return str;
};

Date.parseDate = function(str, fmt) {
	var today = new Date();
	var y = 0;
	var m = -1;
	var d = 0;
	var a = str.split(/\W+/);
	var nofix = false;
	var len = 0;
	if(a[0] == str){
		nofix = true;
		len = str.length;
	}
	var b = fmt.match(/%./g);
	var i = 0, j = 0;
	var hr = 0;
	var min = 0;
	for (i = 0; i < b.length; ++i) {
		switch (b[i]) {
		    case "%d":
		    case "%e":
		    if(nofix&&len>=8)a[i] = str.substring(6,8);
			d = parseInt(a[i], 10);
			break;
		    case "%m":
		    if(nofix&&len>=6)a[i] = str.substring(4,6);
			m = parseInt(a[i], 10) - 1;
			break;
		    case "%Y":
		    case "%y":
		    if(nofix&&len>=4)a[i] = str.substring(0,4);
			y = parseInt(a[i], 10);
			(y < 100) && (y += (y > 29) ? 1900 : 2000);
			break;
		    case "%H":
		    case "%I":
		    case "%k":
		    case "%l":
		    if(nofix&&len>=10)a[i] = str.substring(8,10);
			hr = parseInt(a[i], 10);
			break;
		    case "%P":
		    case "%p":
			if (/pm/i.test(a[i]) && hr < 12)
				hr += 12;
			else if (/am/i.test(a[i]) && hr >= 12)
				hr -= 12;
			break;
		    case "%M":
		    if(nofix&&len>=12)a[i] = str.substring(10,12);
			min = parseInt(a[i], 10);
			break;
		}
	}
	if (isNaN(y)) y = today.getFullYear();
	if (isNaN(m)) m = today.getMonth();
	if (isNaN(d)) d = today.getDate();
	if (isNaN(hr)) hr = today.getHours();
	if (isNaN(min)) min = today.getMinutes();
	if (y != 0 && m != -1 && d != 0)
		return new Date(y, m, d, hr, min, 0);
	y = 0; m = -1; d = 0;
	for (i = 0; i < a.length; ++i) {
		if (a[i].search(/[a-zA-Z]+/) != -1) {
			var t = -1;
			for (j = 0; j < 12; ++j) {
				if (WCalendar._MN[j].substr(0, a[i].length).toLowerCase() == a[i].toLowerCase()) { t = j; break; }
			}
			if (t != -1) {
				if (m != -1) {
					d = m+1;
				}
				m = t;
			}
		} else if (parseInt(a[i], 10) <= 12 && m == -1) {
			m = a[i]-1;
		} else if (parseInt(a[i], 10) > 31 && y == 0) {
			y = parseInt(a[i], 10);
			(y < 100) && (y += (y > 29) ? 1900 : 2000);
		} else if (d == 0) {
			d = a[i];
		}
	}
	if (y == 0)
		y = today.getFullYear();
	if (m != -1 && d != 0)
		return new Date(y, m, d, hr, min, 0);
	return today;
};

//********Select Tools****
function selectAll(n){
	var el = $(":checkbox");
	var vals = [];
	for(var i=0;i<el.length;i++){
		if(el[i].name==n){
			el[i].checked = true;
			vals[vals.length] = el[i].value;
		}
	}
	return vals;
}

function clsSelected(n){
	var el = $(":checkbox");
	for(var i=0;i<el.length;i++){
		if(el[i].name==n){
			el[i].checked = false;
		}
	}
	return [];
}

function inverseSelected(n){
	var el = $(":checkbox");
	var vals = [];
	for(var i=0;i<el.length;i++){
		if(el[i].name==n){
			if(el[i].checked){
				el[i].checked= false;
			}else{
				el[i].checked= true;
				vals[vals.length] = el[i].value;
			}
		}
	}
	return vals;
}

function getSelected(n){
	var el = $(":checkbox");
	var vals = [];
	for(var i=0;i<el.length;i++){
		if(el[i].name==n&&el[i].checked){
			vals[vals.length] = el[i].value;
		}
	}
	return vals;
}

function getUnSelected(n){
	var el = $(":checkbox");
	var vals = [];
	for(var i=0;i<el.length;i++){
		if(el[i].name==n&&!el[i].checked){
			vals[vals.length] = el[i].value;
		}
	}
	return vals;
}

  function delHtmlTag(str){  
        return str.replace(/<[^>]+>/g,"");//去掉所有的html标记  
   }  



function widthCheck(name, maxLength){   
        if(!maxLength){   
            maxLength = 20;   
        }   
        if(name==null||name.length<1){   
            return ["", ""];   
        }   
        var w = 0;//字符串长度，一个汉字长度为2   
        var s = 0;//汉字个数   
        var p = false;//判断字符串当前循环的前一个字符是否为汉字   
        var b = false;//判断字符串当前循环的字符是否为汉字   
        var nameSub;   
        for (var i=0; i<name.length; i++) {   
           if(i>1 && b==false){   
                p = false;   
           }   
           if(i>1 && b==true){   
                p = true;   
           }   
           var c = name.charCodeAt(i);   
           //单字节加1   
           if ((c >= 0x0001 && c <= 0x007e) || (0xff60<=c && c<=0xff9f)) {   
                w++;   
                b = false;   
           }else {   
                w+=2;   
                s++;   
                b = true;   
           }   
           if(w>maxLength && i<=name.length-1){   
                if(b==true && p==true){   
                    nameSub = name.substring(0,i-2)+"...";   
                }   
                if(b==false && p==false){   
                    nameSub = name.substring(0,i-3)+"...";   
                }   
                if(b==true && p==false){   
                    nameSub = name.substring(0,i-2)+"...";   
                }   
                if(p==true){   
                    nameSub = name.substring(0,i-2)+"...";   
                }   
                break;   
           }   
        }   
        if(w<=maxLength){   
            return [name];   
        }   
        return [nameSub];   
    } 
	
function openwindow(url,name,iWidth,iHeight)
 {
  var url;                                 //转向网页的地址;
  var name;                           //网页名称，可为空;
  var iWidth;                          //弹出窗口的宽度;
  var iHeight;                        //弹出窗口的高度;
  var iTop = (window.screen.availHeight-30-iHeight)/2;       //获得窗口的垂直位置;
  var iLeft = (window.screen.availWidth-10-iWidth)/2;           //获得窗口的水平位置;
  window.open(url,name,'height='+iHeight+',,innerHeight='+iHeight+',width='+iWidth+',innerWidth='+iWidth+',top='+iTop+',left='+iLeft+',toolbar=no,menubar=no,scrollbars=auto,resizeable=no,location=no,status=no');
 }

var safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1) ? true : false;
function CustomRadioAncCheckbox() {
    $("body").addClass("has-js");
    var ls = $('label');
    for (var i = 0; i < ls.length; i++) {
        var l = $(ls[i]).get(0);
        if (l.className.indexOf('label_') == -1) continue;
        var inp = $(l).find('input:first').get(0);
        if (l.className == 'label_check' || l.className=='label_check_c c_on_c' || l.className=='label_check_c c_off') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_check c_on' : 'label_check c_off';
            l.onclick = check_it;
        };
        if (l.className == 'label_radio') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off';
            l.onclick = turn_radio;
        };
    };
};
var check_it = function() {
    var inp = $(this).find('input:first').get(0);
    if (this.className == 'label_check c_off' || (!safari && !inp.checked)) {
        this.className = 'label_check c_on';
        if (safari) inp.click();
    } else {
        this.className = 'label_check c_off';
        if (safari) inp.click();
    };
};
var turn_radio = function() {
    var inp = $(this).find('input:first').get(0);
    if (this.className == 'label_radio r_off' || inp.checked) {
        var ls = $(this.parentNode.parentNode.parentNode).find('label');
        for (var i = 0; i < ls.length; i++) {
            var l = $(ls[i]).get(0);
            if (l.className.indexOf('label_radio') == -1)  continue;
            l.className = 'label_radio r_off';
        };
        this.className = 'label_radio r_on';
        if (safari) inp.click();
    } else {
        this.className = 'label_radio r_off';
        if (safari) inp.click();
    };
};



var safari = (navigator.userAgent.toLowerCase().indexOf('safari') != -1) ? true : false;
function CustomRadioAncCheckbox_s() {
	
    $("body").addClass("has-js");
    var ls = $('label');
    for (var i = 0; i < ls.length; i++) {
        var l = $(ls[i]).get(0);
        if (l.className.indexOf('label_') == -1) continue;
        var inp = $(l).find('input:first').get(0);
        if (l.className == 'label_check_c' || l.className=='label_check_c c_on_c' || l.className=='label_check_c c_off') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_check_c c_on_c' : 'label_check_c c_off';
            l.onclick = check_it_s;
        };
        if (l.className == 'label_radio') {
            l.className = (safari && inp.checked == true || inp.checked) ? 'label_radio r_on' : 'label_radio r_off';
            l.onclick = turn_radio_s;
        };
    };
};
var check_it_s = function() {
	
    var inp = $(this).find('input:first').get(0);
    if (this.className == 'label_check_c c_off' || (!safari && !inp.checked)) {
        this.className = 'label_check_c c_on_c';
        if (safari) inp.click();
    }else {
		
        this.className = 'label_check_c c_off';
		
        if (safari) inp.click();
    };
};
var turn_radio_s = function() {
    var inp = $(this).find('input:first').get(0);
    if (this.className == 'label_radio r_off' || inp.checked) {
        var ls = $(this.parentNode.parentNode.parentNode).find('label');
        for (var i = 0; i < ls.length; i++) {
            var l = $(ls[i]).get(0);
            if (l.className.indexOf('label_radio') == -1)  continue;
            l.className = 'label_radio r_off';
        };
        this.className = 'label_radio r_on';
        if (safari) inp.click();
    } else {
        this.className = 'label_radio r_off';
        if (safari) inp.click();
    };
};